<?php
/**
 * Cms_Administrator
 *
 * @category   Cms
 * @package    Cms_Model
 */
class Cms_Model_Administrator extends BIS_Model_Abstract
{

    /**
     * Get Administrator Groups by its id
     *
     * @param  int $id The id
     * @return Cms_Resource_AdministratorGroup_Item|null
     */
    public function getAdministratorGroupById($id)
    {
        $id = (int) $id;

        return $this->getResource('AdministratorGroup')->getAdministratorGroupById($id);
    }

     /**
     * Get All the Administrator Groups
     *
     * @param null|array  $data  sort, order, page, limit
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getAdministratorGroups($data = null)
    {
        if ($data) {

			$sortData = array('name');

                        $order = '';
			if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
				$order .= $data['sort'];
			} else {
				$order .= "name";
			}

			if (isset($data['order']) && ($data['order'] == 'desc')) {
				$order .= " DESC";
			} else {
				$order .= " ASC";
			}

                        $data['order'] = $order;

			if (isset($data['page'])) {
				if ($data['page'] < 0) {
					$data['page'] = 0;
				}
			}

                        if (isset($data['limit'])) {
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
			}else{
                            $data['limit'] = 20;
                        }

			return $this->getResource('AdministratorGroup')->getAdministratorGroups($data);

		} else {
			return $this->getResource('AdministratorGroup')->getAdministratorGroups();
			}
    }

    /**
     * Save Administrator Group
     *
     * @param array  $data The data to insert/update
     * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */
    public function saveAdministratorGroup($data, $row = null)
    {
        if(isset($data['accessPermission'])){
            $data['accessPermission'] = serialize($data['accessPermission']);
        }
        
        if(isset($data['modifyPermission'])){
            $data['modifyPermission'] = serialize($data['modifyPermission']);
        }
        return $this->getResource('AdministratorGroup')->saveRow($data, $row);
    }

    /**
     * Delete Administrator Group
     *
     * @param Zend_DB_Table_Row $row The row to use
     * @return int The number of rows deleted
     */
    public function deleteAdministratorGroup($row)
    {
        return $this->getResource('AdministratorGroup')->deleteRow($row);
    }

    /**
     * Get Administrator by its id
     *
     * @param  int $id The id
     * @return Cms_Resource_Administrator_Item|null
     */
    public function getAdministratorById($id)
    {
        $id = (int) $id;

        return $this->getResource('Administrator')->getAdministratorById($id);
    }

    /**
     * Get All the Administrators
     *
     * @param null|array  $data  sort, order, page, limit
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getAdministrators($data = null)
    {
        if ($data) {

			$sortData = array('surname','username','otherNames','status');

                        $order = '';
			if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
				$order .= $data['sort'];
			} else {
				$order .= "surname";
			}

			if (isset($data['order']) && ($data['order'] == 'desc')) {
				$order .= " DESC";
			} else {
				$order .= " ASC";
			}

                        $data['order'] = $order;

			if (isset($data['page'])) {
				if ($data['page'] < 0) {
					$data['page'] = 0;
				}
			}

                        if (isset($data['limit'])) {
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
			}else{
                            $data['limit'] = 20;
                        }

			return $this->getResource('Administrator')->getAdministrators($data);

		} else {
			return $this->getResource('Administrator')->getAdministrators();
			}
    }

    /**
     * Get Administrator by its username
     *
     * @param  string $username The Username
     * @param  Cms_Resource_Administrator_Item|null $ignoreAdministrator Administrator to ignore from the search
     * @return null|Cms_Resource_Administrator_Item
     */
    public function getAdministratorByUsername($username, $ignoreAdministrator=null)
    {
        return $this->getResource('Administrator')->getAdministratorByUsername($username, $ignoreAdministrator);
    }

    /**
     * Get Administrator by its email
     *
     * @param  string $email The email
     * @param  Cms_Resource_Administrator_Item|null $ignoreAdministrator Administrator to ignore from the search
     * @return null|Cms_Resource_Administrator_Item
     */
    public function getAdministratorByEmail($email, $ignoreAdministrator=null)
    {
        return $this->getResource('Administrator')->getAdministratorByEmail($email, $ignoreAdministrator);
    }

    /**
     * Save Administrator
     *
     * @param array  $data The data to insert/update
     * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */
    public function saveAdministrator($data, $row = null)
    {
        // password hashing
        if (array_key_exists('password', $data) && '' != $data['password']) {
            $data['password'] = md5($data['password']);
        } else {
            unset($data['password']);
        }

        $currentTime = date('Y-m-d H:i:s');
        
        if(null !== $row){
            $data['lastUpdated'] = $currentTime;
            unset($data['adminId']);
        }else{
            $data['dateAdded'] = $currentTime;
        }

        return $this->getResource('Administrator')->saveRow($data, $row);
    }

    /**
     * Set Administrator Last Login
     *
     * param Zend_DB_Table_Row $row The row to use
     * @return mixed The primary key
     */
    public function setAdministratorLastLogin($row)
    {
        $row->lastLogin = date('Y-m-d H:i:s');   
        return $this->getResource('Administrator')->saveRow($row->toArray(), $row);
    }

    /**
     * Delete Administrator
     *
     * @param Zend_DB_Table_Row $row The row to use
     * @return int The number of rows deleted
     */
    public function deleteAdministrator($row)
    {
        return $this->getResource('Administrator')->deleteRow($row);
    }

    /**
     * Get total number of administrators by group Id
     *
     * @param  int $groupId The group id
     * @return int          Total Number of Administrators in the group id
     */
    public function getTotalAdministratorsByGroupId($groupId)
    {
        return $this->getResource('Administrator')->getTotalAdministratorsByGroupId($groupId);
    }

    /**
     * Get total number of administrators by branch Id
     *
     * @param  int $branchId The branch id
     * @return int Total Number of Administrators in a branch
     */
    public function getTotalAdministratorsByBranchId($branchId)
    {
        return $this->getResource('Administrator')->getTotalAdministratorsByBranchId($branchId);
    }

}